/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.maze.display;

import org.maze.Maze;
import org.maze.individual.Individual;

/**
 *
 * @author tpasquie
 */
public class ConsoleDisplay extends AbstractDisplay implements IDisplay{
    

    public void refresh() throws Exception {
        Maze maze = Maze.getInstance();
        byte[] grid = maze.getGrid().clone();
        int size = maze.getSize();

        if(path!=null){
            int last=0;
            for(int pos : path){
                grid[pos]=4;
                last=pos;
            }
            grid[last]=5;
        }
        System.out.print("Generation : ");
        System.out.println(generation);
        // upper border
        for(int i = 0; i < size+2; i++)
            System.out.print("#");
        System.out.print("\n");

        for(int y = 0; y < size; y++){
            System.out.print("#"); //left border
            for(int x = 0; x < size; x++){
                byte field = grid[x+y*size];
                if(field==Maze.EMPTY_FIELD)
                    System.out.print(" ");
                else if(field == Maze.WALL_FIELD)
                    System.out.print("#");
                else if(field == Maze.STARTING_POSITION)
                    System.out.print("S");
                else if(field == Maze.MAZE_EXIT)
                    System.out.print("O");
                else if(field == 4)
                    System.out.print("x");
                else if(field == 5)
                    System.out.print("@");
                else
                    throw new Exception("Unexpected value");
            }
            System.out.print("#\n"); // right border
        }
        //down border
        for(int i = 0; i < size+2; i++)
            System.out.print("#");
        System.out.print("\n");
        System.out.print("Fitest score : ");
        System.out.println(fitest.getFitnessScore());
        System.out.print("Fitest size : ");
        System.out.println(fitest.getPath().size());
        System.out.print("Average score : ");
        System.out.println(average);
        System.out.print("Diversity : ");
        System.out.println(diversity);
        this.displayFitest();
        System.out.print("\n\n");
    }

    private void displayFitest() {
        byte[] genome =fitest.getGenome();
        for(int i = 0; i < genome.length; i++){
            if(genome[i] == Individual.EAST)
                System.out.print("E");
            else if(genome[i] == Individual.WEST)
                System.out.print("W");
            else if(genome[i] == Individual.NORTH)
                System.out.print("N");
            else if(genome[i] == Individual.SOUTH)
                System.out.print("S");
        }
        System.out.println("");
        System.out.println("#####################");
    }

}
